package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 大佬味的小男孩
 * @description:
 * @date 2020/07/21 11:47
 */
@CanalEventListener  //声明当前类是canal监听类
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * @param eventType  当前操作数据库的类型
     * @param rowData 当前操作数据库的数据
     * @return void
     * @description:
     * @author 大佬味的小男孩
     * @date 2020/07/21 11:49
     */
    //对于adupdate方法监听的是changgou_business数据库中的tb_ad表
    @ListenPoint(schema = "changgou_business", table = {"tb_ad"})
    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("tb_ad表的广告数据发生变化");

        //修改前数据
        //List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            if (column.getName().equals("position")) {
                System.out.println("修改前数据:" + column.getValue());
                //System.out.println("发送消息到mq  ad_update_queue:" + column.getValue());
                //rabbitTemplate.convertAndSend("", "ad_update_queue", column.getValue());  //发送消息到mq
                break;
            }
        }

            //修改后数据
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if (column.getName().equals("position")) {
                System.out.println("发送消息到mq  ad_update_queue:" + column.getValue());
                rabbitTemplate.convertAndSend("", "ad_update_queue", column.getValue());  //发送消息到mq
                break;
            }
        }
    }
}
